package com.example.juejincheck.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.juejincheck.bean.*;
import com.example.juejincheck.dao.FeidianConfigDao;
import com.example.juejincheck.dao.JueJinAssignAccountDao;
import com.example.juejincheck.dao.MotianlunAccountDao;
import com.example.juejincheck.dao.RibaoDao;
import com.example.juejincheck.domain.FeidianConfig;
import com.example.juejincheck.domain.JuejinAssignAccount;
import com.example.juejincheck.domain.MotianlunAccount;
import com.example.juejincheck.domain.Ribao;
import com.example.juejincheck.service.JobService;
import com.example.juejincheck.service.MiniprogramService;
import com.example.juejincheck.utils.*;
import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static org.apache.coyote.http11.Constants.a;

/**
 * @author C.W
 * @date 2022/6/1 9:34
 * @desc 掘金签到
 */
@Slf4j
@Component
public class JueJinAssignJob {


    @Resource
    private JobService jobService;
    @Resource
    private JueJinAssignAccountDao jueJinAssignAccountDao;
    @Resource
    private FeidianConfigDao feidianConfigDao;
    @Resource
    private MotianlunAccountDao motianlunAccountDao;
    @Resource
    private RibaoDao ribaoDao;

    @Scheduled(cron = "0 0 09 * * ?")
    public void start() throws InterruptedException {
        List<JuejinAssignAccount> accountPOList = jueJinAssignAccountDao.selectAll();
        List<FeidianConfig> feidianConfigs = feidianConfigDao.selectAll();
        for (JuejinAssignAccount juejinAssignAccountPO : accountPOList) {
            // 发沸点
            int i = RandomUtil.randomInt(0,3);
            log.info("休息"+i+"分钟后处理下一个");
            Thread.sleep(i*60000);
            jobService.publishShortMsg(juejinAssignAccountPO.getCookie());
        }
    }
    @Scheduled(cron = "30 30 20 * * ?")
    public void feidianhuodong() {
        List<JuejinAssignAccount> accountPOList = jueJinAssignAccountDao.selectAll();
        List<FeidianConfig> feidianConfigs = feidianConfigDao.selectAll();
        if(!CollectionUtils.isEmpty(feidianConfigs)){
            FeidianConfig feidianConfig = feidianConfigs.get(0);
            Date today = new Date();
            if(jobService.isEffectiveDate(today, feidianConfig.getStartTime(), feidianConfig.getEndTime())) {
                for (JuejinAssignAccount juejinAssignAccountPO : accountPOList) {
                    jobService.noticeShortMsg(juejinAssignAccountPO, feidianConfig);
                }
            }
        }
    }

    @Scheduled(cron = "0 03 09 * * ?")
    public void checkIn() throws InterruptedException {
        for (MotianlunAccount motianlunAccount : motianlunAccountDao.selectAll()) {
            int i = RandomUtil.randomInt(1,10);
            log.info("休息"+i+"分钟后处理下一个");
            Thread.sleep(i*60000);
            jobService.assign(motianlunAccount);
        }
    }

    @Scheduled(cron = "0 30 17 * * ?")
    public void ribao() throws InterruptedException {
        for (Ribao ribao : ribaoDao.selectAll()) {
            if(!StringUtils.isEmpty(ribao.getPushPlusToken())){
                StringBuilder html = new StringBuilder();
                Map<String,Object> map = new HashMap<>();
                map.put("token", ribao.getPushPlusToken());
                map.put("title","今天日报写了吗？");

                html.append("<a href='").append(ribao.getUrl()).append("'>").append(ribao.getUrl()).append("</a>");
                map.put("content",html.toString());
                log.info("参数：{}", map);
                String msgUrl = HttpUtil.post("https://www.pushplus.plus/send/",map);
                log.info("发送消息：{}", msgUrl);
            }
        }
    }
//    @Scheduled(cron = "0 20 05 * * ?")
    public void sayMorning() {
        jobService.sendWechatToUser();
    }
}
